package com.maxsalad.eleme.service;

import com.maxsalad.common.util.JsonUtil;
import com.maxsalad.common.util.OkHttpUtil;
import com.maxsalad.eleme.entity.order.detail.ResponseOrderDetailEntity;
import com.maxsalad.eleme.entity.order.list.ResponseOrderListEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by 2014-400 on 2016-03-07.
 */
@Service(value = "elemeOrderService")
public class OrderService extends BaseService {
    private static final Logger log = LoggerFactory.getLogger(OrderService.class);

    /**
     * 获取新的订单
     *
     * @return
     * @throws Exception
     */
    public ResponseOrderListEntity getNewOrder() throws Exception {
        String url = getBaseUrl() + "order/new/";

        Map<String, String> params = new HashMap<String, String>();
        params.put("restaurant_id", getRestaurantId());

        url = getRequestUrl(url, params, null);
        log.debug(url);

        String json = OkHttpUtil.get(url);
        log.debug(json);

        return (ResponseOrderListEntity) JsonUtil.deserialize(json, ResponseOrderListEntity.class);
    }

    /**
     * 获取订单详情
     *
     * @param id
     * @return
     * @throws Exception
     */
    public ResponseOrderDetailEntity getOrderDetail(int id) throws Exception {
        String url = getBaseUrl() + "order/" + id + "/";

        Map<String, String> params = new HashMap<String, String>();
        params.put("tp_id", "1");//tp_id如果是1，那么返回的食物的ID将会附带上商户提供的食物、食物分类和店铺ID，商户可以根据此ID进行接单

        url = getRequestUrl(url, params, null);
        log.debug(url);

        String json = OkHttpUtil.get(url);
        log.debug(json);

        return (ResponseOrderDetailEntity) JsonUtil.deserialize(json, ResponseOrderDetailEntity.class);
    }
}
